//
//  UITableView+LRRefresh.m
//  LRShop
//
//  Created by HCW on 16/8/6.
//  Copyright © 2016年 HCW. All rights reserved.
//

#import "UIScrollView+LRRefresh.h"
#import "MJRefresh.h"

@implementation UIScrollView (LRRefresh)

- (void)setRefreshWithHeaderBlock:(void(^)())headerBlock
                      footerBlock:(void(^)())footerBlock
{
    if (headerBlock) {
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            if (headerBlock) {
                headerBlock();
            }
        }];
        NSMutableArray *idleImages = [NSMutableArray array];
        
        //这块为你刷新时出现的gif图 需要循环添加到数组中
        
        for (NSUInteger i = 1; i <= 4; i++) {
            
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0%lu.png", (unsigned long)i]];
            
            [idleImages addObject:image];
            
        }
        
        [header setImages:idleImages forState:MJRefreshStateIdle];
        
        header.refreshingTarget = idleImages;
        NSMutableArray *refreshingImages = [NSMutableArray array];
        
        for (NSUInteger i = 1; i <= 4; i++) {
            
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0%lu.png", (unsigned long)i]];
            
            [refreshingImages addObject:image];
            
        }
        
        [header setImages:refreshingImages forState:MJRefreshStatePulling];
        
   
    
        header.lastUpdatedTimeLabel.hidden = YES;
        header.stateLabel.hidden = YES;
        self.mj_header = header;
    }
    
    if (footerBlock) {
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            footerBlock();
        }];
        [footer setTitle:kNoMoreData forState:MJRefreshStateNoMoreData];
        [footer setTitle:@"没有更多数据" forState:MJRefreshStateIdle];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [footer setTitle:@"" forState:MJRefreshStateIdle];
        });
        footer.tintColor = [Utils colorWithHexString:@"dddddd"];
        footer.refreshingTitleHidden = YES;
        self.mj_footer = footer;
    }
    
}

- (void)headerBeginRefreshing
{
    [self.mj_header beginRefreshing];
}

- (void)headerEndRefreshing
{
    [self.mj_header endRefreshing];
}

- (void)footerEndRefreshing
{
    [self.mj_footer endRefreshing];
}

- (void)footerNoMoreData
{
    [self.mj_footer setState:MJRefreshStateNoMoreData];
}

@end
